package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.pojo.vo.DetailsVo;
import com.dongdongshop.pojo.vo.GoodsVo;
import com.dongdongshop.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("goodsController")
public class GoodsController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(SellerController.class);

    @Reference(check=false ,timeout=1006666)
    private GoodsService goodsService;

    //跳转到商品审核页面
    @RequestMapping("goodsList")
    public String goodsList(){
        return "admin/goods";
    }
    //获取商品审核的商品信息
    @RequestMapping("getGoodsInfo")
    @ResponseBody
    public Result getGoodsInfo(String goodsName){
        try {
            logger.info("获取商品信息方法开始运行{}");
            List<GoodsVo> goodsInfo =goodsService.getGoodsShInfo(goodsName);
            return Result.response(ResultEnum.SUCCESS).setData(goodsInfo);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]")Long[] ids){
        try {
            logger.info("商品批量删除方法开始执行{}");
            goodsService.deleteBatch(ids);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //提交审核
    @RequestMapping("SubmitReview")
    @ResponseBody
    public Result SubmitReview(@RequestParam("ids[]")Long[] ids,String statu){
        try {
            logger.info("商品提交审核方法开始执行{}");
            goodsService.SubmitReview(ids ,statu);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //商品详情
    @RequestMapping("getDetailsById")
    @ResponseBody
    public Result getDetailsById(Long id){

        try {
            logger.info("商品提交审核方法开始执行{}");
            List<DetailsVo> detailsVos = goodsService.getDetailsById(id);
            return Result.response(ResultEnum.SUCCESS).setData(detailsVos);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
}
